﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;
using System.Windows.Forms;
using System.Collections;


namespace WLC
{
    class GetStatisticValueStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        public IStep CreateStep(IPropertyReaders properties)
        {
            return new GetStatisticValueStep(properties);
        }

        public void DefineSchema(IPropertyDefinitions schema)
        {
            IPropertyDefinition statisticElement = schema.AddElementProperty("StatisticElement", StatisticElementDefinition.ID);
            statisticElement.Description = "The statistics element that manages the statistics for this model.";
            statisticElement.Required = true;

            IPropertyDefinition statisticName = schema.AddStringProperty("StatisticName", string.Empty);

            statisticName.Description = "The name of the statistic for which to get the current value.";
            statisticName.Required = true;


            IPropertyDefinition saveToState = schema.AddStateProperty("SaveToState");
            saveToState.Description = "The state variable to save the statistic's value to.";
            saveToState.Required = true;

            IPropertyDefinition valueType = schema.AddStringProperty("ValueType", "avg");
            valueType.Description = "The value type to return. Possible values are 'avg', 'min', 'max', 'last', or 'deviation'.";
            valueType.Required = false;

        }

        public int NumberOfExits
        {
            get { return 1; }
        }


        public string Name
        {
            get { return "GetStatisticValue"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }



        public Guid UniqueID
        {
            get { return new Guid("{9FFE3A46-6EFA-11DF-A63C-FA13E0D72085}"); }
        }

        #endregion
    }


    class GetStatisticValueStep : IStep
    {

        IPropertyReader _statisticName;
        IStateProperty _saveToStateProp;
        IElementProperty _statisticElementProp;
        IPropertyReader _valueTypeProp;


        public GetStatisticValueStep(IPropertyReaders bag)
        {
            _statisticElementProp = (IElementProperty)bag.GetProperty("StatisticElement");
            _statisticName = bag.GetProperty("StatisticName");
            _saveToStateProp = (IStateProperty)bag.GetProperty("SaveToState");
            _valueTypeProp = bag.GetProperty("ValueType");



        }

        #region IStep Members

       
        public ExitType Execute(IStepExecutionContext context)
        {
       
            string valueType = _valueTypeProp.GetStringValue(context);

            string statisticName = _statisticName.GetStringValue(context);

            StatisticElement statisticElement = (StatisticElement)_statisticElementProp.GetElement(context);
                       
            // Convert string to Enum
            StatisticElement.ValueTypes valueTypeEnum = (StatisticElement.ValueTypes)Enum.Parse(typeof(StatisticElement.ValueTypes), valueType);

            
            double value = statisticElement.GetStatisticValue(statisticName, valueTypeEnum);

            // Finally, set the value to the state variable to return it to Simio.
            _saveToStateProp.GetState(context).StateValue = value;

            return ExitType.FirstExit;
        }

        #endregion

    }


}
